<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use DiDom\Document;
use DiDom\Query;
use App\Model\WebBaike;
use App\Model\WebQue;
use App\Model\WebAnw;
use App\Model\WebUsername;
use App\Model\Fangzhou;
use App\Model\FangzhouWord;
use App\Model\FangzhouDetail;
use App\Model\FangzhouDetailBaidu;
use App\Model\FangzhouDetailSo;
use App\Model\FangzhouDetailIask;
use App\Model\Xiaoqu;
use App\Model\Keyword;
use App\Model\Loacl\Baike;
use App\Services\OSS\OSS;
class HomeController extends Controller
{
    private $_no_num = 0; 
    public function index(Request $request){
      ini_set("max_execution_time", 300); 
      $second = 280;
      if(isset($_GET['action'])){
            if($_GET['action']=='baidu'){
              $this->get_baidu_detail($_GET['begin'],$_GET['end'],$second);
            }else if($_GET['action']=='iask'){
              $this->get_iask_detail($_GET['begin'],$_GET['end'],$second);
            }else if($_GET['action']=='360'){
              $this->get_360_detail($_GET['begin'],$_GET['end'],$second);
            }
      
            // if($_GET['action']=='bjxiaoqu'){
            //   $this->get_beijing_xiaoqu(1,590);
            // }
            // if($_GET['action']=='shxiaoqu'){
            //   $this->get_shanghai_xiaoqu(1,590);
            // }
      }else{
         //$this->get_baidu_detailtext();
         $this->get_baidu_detail(1519,1521,10);
        
      }
    }

    public function get_baidu_detailtext(){
          $baidu_url = $this->get_baidu_url2("燃气热水器多少钱");
          foreach ($baidu_url as $key => $value) {  
              $url ="https://zhidao.baidu.com".$value;
              echo $url;
              echo "<br>";
          }
    } 
    
    public function get_baidu_url2($word){
      $word = urlencode($word);
      $url = "http://zhidao.baidu.com/search?word=".$word;
      $document = new Document($url, true);      
      $match = "/(\/question.*)\?/U";
      preg_match_all($match, $document->html(), $baidu_url);
      return array_unique($baidu_url[1]);
    }

    public function checkBaiduDetail(){
      $FangzhouDetailBaidu = FangzhouDetailBaidu::select("key_id","keyword")->get();
      $dataArr = [] ; 
      foreach ($FangzhouDetailBaidu as $key => $value) {
        if(!in_array($value->keyword, $dataArr)){
          $dataArr[] = $value->keyword;
          // $FangzhouWord = new FangzhouWord();
          // $FangzhouWord->keyword =$value->keyword;
          // $FangzhouWord->key_id =$value->key_id;
          // $FangzhouWord->save(); 
        }
     
      }
      print_r($dataArr);
      
    }
     public function get_shanghai_xiaoqu($begin,$endnum){        
      $city = '上海'; 
      $Xiaoqu =Xiaoqu::select('page')->orderBy('page','desc')
                        ->where('city',$city)->where('page','<',$endnum)->where('page','>=',$begin)
                        ->orderBy('page','desc')->first();  
      if($Xiaoqu){ 
        $begin = $Xiaoqu->page>=$begin?$Xiaoqu->page:$begin;         
      }           
      $begintime = time();
      
      for($i =$begin;$i<$endnum;$i++){
        if (time() - $begintime>280) {
              \Log::info('==================我因超时而死亡======='.$begin.'======');
               die();
        }     
        $url = "http://sh.esf.leju.com/community/n".$i; 
        $document = new Document($url, true);    
        $html = $document->find(".community-main-one a::attr(href)");
        if(isset($html[0])){
          foreach ($html as $key => $url) {     
            $match = "/info\/(.*)\/#/";
            preg_match($match, $url,$arr);
            $ar_id = $arr[1];
            if($ar_id){
              $Xiaoqu_repeat =Xiaoqu::where('leju_id',$ar_id)->where('city',$city)->first();
              if($Xiaoqu_repeat){
                    \Log::info('==================我因重复而中断======='.$ar_id.'======');
                    break;
              }
              $this->get_leju_detail($url,$ar_id,$city,$i);
              usleep(20000000);  //睡眠20s
            }
          }
        }
      }   
    }
    public function get_beijing_xiaoqu($begin,$endnum){        
      $city = '北京';  
      $Xiaoqu =Xiaoqu::select('page')->orderBy('page','desc')
                        ->where('city',$city)->where('page','<',$endnum)->where('page','>=',$begin)
                        ->orderBy('page','desc')->first();  
      if($Xiaoqu){ 
        $begin = $Xiaoqu->page>=$begin?$Xiaoqu->page:$begin;         
      }   

      $begintime = time();
      for($i =$begin;$i<$endnum;$i++){
        if (time() - $begintime>280) {
              \Log::info('==================我因超时而死亡======='.$begin.'======');
               die();
        }     
        $url = "http://bj.esf.leju.com/community/n".$i; 
        $document = new Document($url, true);    
        $html = $document->find(".community-main-one a::attr(href)");
    
        if(isset($html[0])){
          foreach ($html as $key => $url) {     
            $match = "/info\/(.*)\/#/";
            preg_match($match, $url,$arr);
            $ar_id = $arr[1];
            if($ar_id){
              $Xiaoqu_repeat =Xiaoqu::where('leju_id',$ar_id)->where('city',$city)->first();
              if($Xiaoqu_repeat){
                    \Log::info('==================我因重复而中断======='.$ar_id.'======');
                    break;
              }
              $this->get_leju_detail($url,$ar_id,$city,$i);
              usleep(20000000);  //睡眠20s
            }
          }
        }
      }   
    }
    public function get_leju_detail($url,$ar_id,$city,$page){  
        $document = new Document($url, true);         
        if(isset($document->find('.h-text')[0])){
         // print_r($document->find(".sub-tit")[0]->html());
      
            $district = $document->find('h1')[0]->text();   //小区名称
            $city_html= $document->find('.h-text')[0];   
            $area = $city_html->find(".add_s0")[0]->text();
            $city_district_arr = explode("-", $area);
            if(count($city_district_arr)){
              $city_district = $city_district_arr[0];
              $detail_district = $area;
            }else{
              $city_district = $area;    //城区
              $detail_district = $area; //详细区域
            } 
            $address = $city_html->find(".add_s0_e")[0]->text();           
            $age =$city_html->find(".panelB tr")[0]->text();
            $match = "/[0-9]+/";
            preg_match_all($match, $age, $matches);
            if(isset($matches[0][0])){
              $age = $matches[0][0];
              $type = $city_html->find(".panelB tr em")[0]->text();  
              $new_xiaoqu  = new Xiaoqu();
              $new_xiaoqu->leju_id= $ar_id;
              $new_xiaoqu->city= $city;
              $new_xiaoqu->page= $page;
              $new_xiaoqu->district= $district;
              $new_xiaoqu->city_district= $city_district;
              $new_xiaoqu->detail_district= $detail_district;
              $new_xiaoqu->address= $address;
              $new_xiaoqu->type= $type;
              $new_xiaoqu->age= $age;
              $new_xiaoqu->lianjia_url= $url;
              $new_xiaoqu->save();

            }
            
        } 
    }
    public function get_wenda_detail($minNum=0,$maxNum=10000,$num=10000){
      $Fangzhou = Fangzhou::orderBy('id','asc')->first();
      // $baidu_url = json_decode($Fangzhou->baidu_url);
      // foreach ($baidu_url as $key => $value) {
      //   $url ="https://zhidao.baidu.com/".$value;
      //   FangzhouDetailBaidu::get_baidu_detail($Fangzhou->keyword,$url);
      //   usleep(500000);  //睡眠0.5s
      // }  
      // $iask_url = json_decode($Fangzhou->iask_url);
      // foreach ($iask_url as $key => $value) {
      //   $url = "http://m.iask.sina.com.cn/b/".$value.".html";
      //   FangzhouDetailIask::get_iask_detail($Fangzhou->keyword,$value,$url);
      //   usleep(500000);  //睡眠0.5s
      // }
      $m_360_url =  json_decode($Fangzhou->m_360_url);
      foreach ($m_360_url as $key => $value) {  
        $url = "http://m.wenda.so.com".$value;
        // FangzhouDetailSo::get_360_detail($Fangzhou->keyword,$url);   
        echo time(); 
        echo "<br>";
         usleep(1000000);  //睡眠0.5s
         echo time();
         die();
      }
    }
      public function get_360_detail($minNum=110,$maxNum=10000,$second=280){
      $FangzhouDetailSo = FangzhouDetailSo::select('key_id')->where('key_id','>',$minNum)->where('key_id','<',$maxNum)->orderBy('key_id','desc')->first();   
      $key_id = $FangzhouDetailSo?$FangzhouDetailSo->key_id+1:$minNum;
      $begintime = time();      
      for($i =$key_id;$i<$maxNum;$i++){
        if (time() - $begintime>$second) {
              \Log::info('==================我因超时而死亡======='.$key_id.'======');
               die();
        }      
        $Fangzhou = FangzhouWord::where('id','>=',$i)->orderBy('id','asc')->first();
        if($Fangzhou){
          
          $m_360_url =  $this->get_360_url($Fangzhou->keyword); 
       
          foreach ($m_360_url as $key => $value) {
            $url = "http://m.wenda.so.com".$value;
            FangzhouDetailSo::get_360_detail($Fangzhou->keyword,$Fangzhou->id,$url); 
            usleep(2000000);  //睡眠1s
          }
        }
      }      
      $m_360_url =  json_decode($Fangzhou->m_360_url);     
    }
    public function get_baidu_detail($minNum=110,$maxNum=10000,$second=280){
      $FangzhouDetailBaidu = FangzhouDetailBaidu::select('key_id')->where('key_id','>=',$minNum)->where('key_id','<',$maxNum)->orderBy('key_id','desc')->first();   
      $key_id = $FangzhouDetailBaidu?$FangzhouDetailBaidu->key_id+1:$minNum;
      $begintime = time();   
      for($i =$key_id;$i<$maxNum;$i++){
        if (time() - $begintime>$second) {
              \Log::info('==================我因超时而死亡======='.$key_id.'======');
               die();
        }  
        $Fangzhou = FangzhouWord::select('id','keyword')->where('id','>=',$i)->orderBy('id','asc')->first();   
    
        if($Fangzhou){
         
          $baidu_url = $this->get_baidu_url($Fangzhou->keyword);
          
       
          foreach ($baidu_url as $key => $value) {   

              $url ="https://zhidao.baidu.com".$value;
              FangzhouDetailBaidu::get_baidu_detail($Fangzhou->keyword,$Fangzhou->id,$url);
              usleep(2000000);  //睡眠1s
          }
      
        }  
      }    
    }
  
    public function get_iask_detail($minNum=110,$maxNum=10000,$second=280){
      $FangzhouDetailIask = FangzhouDetailIask::select('key_id')->where('key_id','>',$minNum)->where('key_id','<',$maxNum)->orderBy('key_id','desc')->first();   
      $key_id = $FangzhouDetailIask?$FangzhouDetailIask->key_id+1:$minNum;
      $begintime = time();
      for($i =$key_id;$i<$maxNum;$i++){
        if (time() - $begintime>$second) {
              \Log::info('==================我因超时而死亡======='.$key_id.'======');
               die();
        }      
        $Fangzhou = FangzhouWord::where('id','>=',$i)->orderBy('id','asc')->first();
        if($Fangzhou){
           $iask_url = $this->get_iask_url($Fangzhou->keyword);
          foreach ($iask_url as $key => $value) {  
            $url = "http://m.iask.sina.com.cn/b/".$value.".html";
            FangzhouDetailIask::get_iask_detail($Fangzhou->keyword,$value,$Fangzhou->id,$url);
            usleep(2000000);  //睡眠1s
          }
        }
      }    
    }



    public function get_baidu_wenda($minNum=0,$maxNum=10000,$num=10000){ 

    $key_id = Fangzhou::select('key_id')->where('key_id','>',$minNum)->where('key_id','<',$maxNum)->orderBy('key_id','desc')->first();
    $minNum =$key_id->key_id;    
    $maxNum = $maxNum-10000>$minNum?$minNum+10000:$maxNum;  
    $keyword = Keyword::where('id','>',$minNum)->where('id','<',$maxNum)->where('id','<',$maxNum)->take($num)->orderBy('id','asc')->get();
    $begintime = time();
    foreach ($keyword as $key => $value) {

      if (time() - $begintime>280) {
            \Log::info('==================我因超时而死亡======='.$minNum.'======');
             die();
      }
     if($value->keywords){
        $word =$value->keywords;
        if($word){
          $Fangzhou = Fangzhou::where('keyword',$word)->first(); 

          if(!$Fangzhou){          
            $word = urlencode($value->keywords);
            $baidu_url = $this->get_baidu_url($word);
            $m360_url = $this->get_360_url($word);
            $iask_url = $this->get_iask_url($word);
            $word = urldecode($word);       
            if(count($baidu_url)||count($m360_url)||count($iask_url)){
              $Fangzhou = new Fangzhou();
              $Fangzhou->keyword = $word;
              $Fangzhou->key_id = $value->id;
              $Fangzhou->baidu_url = json_encode($baidu_url);
              $Fangzhou->m_360_url = json_encode($m360_url);
              $Fangzhou->iask_url = json_encode($iask_url);
              $Fangzhou->save();
            }
          } 
        }
     }
    }

    
    }
    public function get_iask_url($word){
      $word = urlencode($word);
      $url = "http://m.iask.sina.com.cn/search/1.html?content=".$word;  
      $document = new Document($url, true);  
      $match = "/quetionDetail\(\'(.*)\'\)/";
      preg_match_all($match, $document->html(), $iask_url);   
      return $iask_url[1];
    }
    public function get_360_url($word){
      $word = urlencode($word);
      $url = "http://m.wenda.so.com/search/?q=".$word;  
      $document = new Document($url, true);  
      $match = "/(\/q.*)\"/U"; 
      preg_match_all($match, $document->html(), $m360_url);   
      return $m360_url[1];
    }
    public function get_baidu_url($word){
      $word = urlencode($word);
      $url = "http://zhidao.baidu.com/search?word=".$word;
      $document = new Document($url, true);      
      $match = "/(\/question.*)\?/U";
      preg_match_all($match, $document->html(), $baidu_url);
      return array_unique($baidu_url[1]);
    }


    public function get_username(){
      $begintime = time();
      $begin = 8;
      $WebUsername =WebUsername::select('name_id')->orderBy('name_id','desc')->first();
        if($WebUsername){
          $begin = $WebUsername->name_id>=$begin?$WebUsername->name_id+1:$begin;         
        }
      \Log::info('==================begin name================='.$begin);
      for ($i=$begin; $i < 1000000; $i++) {
          if (time()-$begintime>290) {
            break;
          }
         $url = "http://www.dianping.com/member/$i";
         $document = new Document($url, true);
         if(isset($document->find(".name")[0])){
          $name = $document->find(".name")[0]->text();
           $WebUsername = new WebUsername();
           $WebUsername->name = $name;
           $WebUsername->name_id = $i;
           $WebUsername->save();
         }    
      }      
    }
    public function getto8toask_more($begin,$endnum){
      $begintime = time();
      $from = 'to8to';    
      $WebQue =WebQue::select('ask_id')->orderBy('id','desc')
                        ->where('from',$from)->where('ask_id','<',$endnum)->where('ask_id','>',$begin)
                        ->orderBy('ask_id','desc')->first();
             echo $begin;
      if($WebQue){
          $begin = $WebQue->ask_id>=$begin?$WebQue->ask_id+1:$begin;         
      }

      for($i =$begin;$i<$endnum;$i++){ 
          if (time() - $begintime>250) {
              \Log::info('==================我因超时而死亡======='.$begin.'======');
               die();
          }else{
            $WebQue_first =WebQue::where('ask_id',$i)->first();
            if($WebQue_first){
              \Log::info('==================我因重复而死亡======='.$begin.'======');
              die();
            }else{
                 $url ="http://www.to8to.com/ask/k".$i.".html";
                      //  $url ="http://192.168.3.151";

                  $document = new Document($url, true);
        
                  $base_data = $document->find('.global_pie'); 
      
                  \Log::info('==================开始文章k======='.$i.'======');   
                    print_r($document);
          
                    echo $url;
                  die();     
                  if(isset($base_data[0])){
                  \Log::info('==================获得文章k======='.$i.'======');   
                   
                  //    \Log::info('==================获得文章k======='.$i.'======');  
                    $navarray =  $base_data[0]->find("li"); 
                    $sign_id = time().$i;
                    $nav = '';
                    foreach ($navarray as $key => $value) {         
                      $nav .= $value->text().' > ';
                    }
                    $ask_qustion = $document->find('.ask_qustion')[0];
                    $ques = $ask_qustion->find('.title3')[0]->html(); 
                    if(isset($ask_qustion->find('.ask_one_p')[0])){
                      $ques_des =$ask_qustion->find('.ask_one_p')[0]->html(); 
                    }else{
                      $ques_des='';
                    }
                    $WebQue = new WebQue();
                    $WebQue->ask_id = $i;
                    $WebQue->sign_id = $sign_id;
                    $WebQue->title = $ques;
                    $WebQue->des= $ques_des;
                    $WebQue->from= $from;
                    $WebQue->nav= trim($nav);
                    $WebQue->url= $url;
                    $WebQue->save();            
                    $Que_id =  $WebQue->id;
                    $bestData =$document->find('.best_answer'); 
                    if (isset($bestData[0])) {
                      $best_answer =$bestData[0]->find('.ask_one_p')[0]->text();        
                      $WebAnw = new WebAnw();
                      $WebAnw->que_id = $Que_id;
                      $WebAnw->sign_id = $sign_id;
                      $WebAnw->isbest = 1;
                      $WebAnw->content = $best_answer;           
                      $WebAnw->save();
                    }               
                    $otherAnswer = $document->find('.ask_answer_li');   
                      if(count($otherAnswer)){
                        foreach ($otherAnswer as $key => $value) {
                            if($key>0){               
                              $answer = $value->find("p")[0]->text();
                              $WebAnw = new WebAnw();
                              $WebAnw->que_id = $Que_id;
                              $WebAnw->sign_id = $sign_id;
                              $WebAnw->content = $answer;  
                              $WebAnw->save();
                            }
                        }
                      }
                }else{
                  $this->_no_num ++;
                  if($this->_no_num>10){    
                  $i = $i+rand(1000,20000);   
                   \Log::info('==================遇到空文区 i+rand======='.$i.'======');               
                   $this->_no_num = 0;
                  }
                  
                }
            }
          }
         
        }
        \Log::info('==================我因无数可读而死亡======='.$begin.'======');
}






  public function getto8toask(){
      $from = 'to8to';
      $begin = 20;
      $endnum = 7541039;
      $WebQue =WebQue::select('ask_id')->orderBy('id','desc')->where('from',$from)->first();
      if($WebQue){
          $begin = $WebQue->ask_id>=$begin?$WebQue->ask_id+1:$begin;         
      }
      echo $begin;
      for($i =$begin;$i<$endnum;$i++){          
          $url ="http://www.to8to.com/ask/k".$i.".html";
          $document = new Document($url, true);
          $base_data = $document->find('.global_pie');          
          if(isset($base_data[0])){
            $navarray =  $base_data[0]->find("li"); 
            $sign_id = time().$i;
            $nav = '';
            foreach ($navarray as $key => $value) {         
              $nav .= $value->text().' > ';
            }
            $ask_qustion = $document->find('.ask_qustion')[0];
            $ques = $ask_qustion->find('.title3')[0]->text(); 
            if(isset($ask_qustion->find('.ask_one_p')[0])){
              $ques_des =$ask_qustion->find('.ask_one_p')[0]->text(); 
            }else{
              $ques_des='';
            }
            $WebQue = new WebQue();
            $WebQue->ask_id = $i;
            $WebQue->sign_id = $sign_id;
            $WebQue->title = $ques;
            $WebQue->des= $ques_des;
            $WebQue->from= $from;
            $WebQue->url= $url;
            $WebQue->save();            
            $Que_id =  $WebQue->id;
            $username[] = $document->find('.ask_qustion_people')[0]->find('b')[0]->text();
            $bestData =$document->find('.best_answer'); 
            if (isset($bestData[0])) {
              $best_answer =$bestData[0]->find('.ask_one_p')[0]->text();
              $username[]  =$bestData[0]->find(".company_name")[0]->text();
              $WebAnw = new WebAnw();
              $WebAnw->que_id = $Que_id;
              $WebAnw->sign_id = $sign_id;
              $WebAnw->isbest = 1;
              $WebAnw->content = $best_answer;           
              $WebAnw->save();
            }               
            $otherAnswer = $document->find('.ask_answer_li');   
              if(count($otherAnswer)){
                foreach ($otherAnswer as $key => $value) {
                    if($key>0){
                      $username[] = $value->find(".company_name")[0]->text();
                      $answer = $value->find("p")[0]->text();
                      $WebAnw = new WebAnw();
                      $WebAnw->que_id = $Que_id;
                      $WebAnw->sign_id = $sign_id;
                      $WebAnw->content = $answer;  
                      $WebAnw->save();
                    }
                }
              }
            if(count($username)){
              foreach($username as $key=>$user){
                if($user){
                  $username = new WebUsername();
                  $username->name=$user;
                  $username->save();
                }
                
              }
            }
            break;
            }
        
        }
}

   private function deleteimg(){
      for ($i=0; $i <20000 ; $i++) {
        $WebBaike =WebBaike::select('id','content')->where('from','to8to')->where('delete_log',0)->orderBy('id','desc')->first();
        if($WebBaike){
            $match = "/beijing.aliyuncs.com\/(.*)jpg[\"]+/";
            preg_match_all($match, $WebBaike->content, $matches);
            foreach ($matches[1] as $key => $value) {
              $url = substr($value,0,-1).'.jpg';

              try{
                $newcontent = preg_replace('/zfnb-mis-imgs/', 'zfnb-zhufaner-com',$WebBaike->content);
                $WebBaike = WebBaike::find($WebBaike->id);
                $WebBaike->content = $newcontent;
                $WebBaike->delete_log = 1;
                $WebBaike->save();
                OSS::moveObject('zfnb-mis-imgs',$url,'zfnb-zhufaner-com',$url);
               OSS::publicDeleteObject('zfnb-mis-imgs',$url);

              }catch(Exception $e) 
              { 
                continue;
              }
            }
        }
      }
    
    }


  private function to8to(){
      $from = 'to8to';
      $begin = 1;
      $endnum =28500;
      $WebBaike =WebBaike::select('bkid')->orderBy('id','desc')->where('from',$from)->first();
      if($WebBaike){
          $begin = $WebBaike->bkid>=$begin?$WebBaike->bkid+1:$begin;         
      }
      for($i =$begin;$i<$endnum;$i++){
        $url = "http://www.to8to.com/baike/$i";
        $document = new Document($url, true);
        
        $navarray = $document->find('.small-nav-wrap')[0]->find(".nav-block"); 

        if(isset($navarray[0])){
            $nav= '';
            foreach ($navarray as $key => $item) {
              if($key>1){
                $nav.='>'.$item->text();
              }
            }
            $title = $document->find('.bk-detail-content-title')[0]->text();
            $abstract = $document->find('.bk-detail-content-intro-right-small')[0]->text();
            $content = $document->find('.bk-detail-content')[0]->html();
            $content .= $document->find('.bk-detail-xgt')[0]->html();
            $match ="/>.*\&gt;\&gt;/U";
            $content= preg_replace($match, '', $content);
            $match ="/<[\/]?[a|h1|h2|strong](.*)>/U";
            $content  = preg_replace($match, '', $content);
            $match ="/<p(.*)>/U";
            $content= preg_replace($match, '<p>', $content);
            $content= $this->downRemoutPic($content);
            break;
            $createWebBaike =new WebBaike();
            $createWebBaike->bkid =$i;
            $createWebBaike->title =$title;
            $createWebBaike->url =$url;
            $createWebBaike->abstract =$abstract ;
            $createWebBaike->content =$this->downRemoutPic($content);
            $createWebBaike->nav =$nav;
            $createWebBaike->from =$from;
            $createWebBaike->save();
            break;
        }
      }
    }
    private function getqijia(){
      $from = 'qijia';
      $begin = 1471;
      $endnum = 8500;
      $WebBaike =WebBaike::select('bkid')->orderBy('id','desc')->where('from',$from)->first();
      if($WebBaike){
          $begin = $WebBaike->bkid>=$begin?$WebBaike->bkid+1:$begin;         
      }
        $begin = 2048;
          for($i =$begin;$i<$endnum;$i++){
              $url = "http://www.jia.com/baike/bdetail-$i/";
              $document = new Document($url, true);
              $nav = $document->find('.bk-nav'); 
              if(isset($nav[0])){
               $nav= $nav[0]->text();
              $title = $document->find('h1')[0]->text(); 
              $contents = $document->find('.atical-floor'); 
              $abstract =  $contents[0]->find("i")[0]->text();
              $num = count($contents);          
              $content='';
              for ($j=1; $j <$num-1 ; $j++) { 
                  if(isset($contents[$j]->find(".floor-node")[0])){
                     $content .= $contents[$j]->find(".floor-node")[0]->html();
                     $content .= $contents[$j]->find(".txt")[0]->html();
                  }
                  if(isset($contents[$j]->find("img")[0])){
                     $content .= $contents[$j]->find("img")[0]->html();
                  }
              }
               
               
              $match ="/<[\/]?[a|h1|h2|strong](.*)>/U";
              $content  = preg_replace($match, '', $content);
              $match ="/<p(.*)>/U";
              $content= preg_replace($match, '<p>', $content);
    
              $createWebBaike =new WebBaike();
              $createWebBaike->bkid =$i;
              $createWebBaike->title =$title;
              $createWebBaike->url =$url;
              $createWebBaike->abstract =$abstract ;
              $createWebBaike->content =$this->downRemoutPic($content);
              $createWebBaike->nav =$nav;
              $createWebBaike->from =$from;
              $createWebBaike->save();
            
             } 
           }
        
    }
    private function gettobosu(){
      $from = 'tobosu';
      $begin = 12;
      $endnum = 8500;
      $WebBaike =WebBaike::select('bkid')->orderBy('id','desc')->where('from',$from)->first();
      if($WebBaike){
          $begin = $WebBaike->bkid>=$begin?$WebBaike->bkid+1:$begin;         
      }
      $WebBaike = 26;
      for($i =$begin;$i<$endnum;$i++){
          $url = "http://baike.tobosu.com/view/n$i.html/";
          $document = new Document($url, true);
          if(isset($document->find('.ency-detail')[0])){
            $nav = $document->find('.ed-cur-page')[0]->find(".fl"); 
            $title = $document->find(".edw-intro")[0]->find('h1')[0]->text();
            $abstract = $document->find(".edw-intro")[0]->find('.intro')[0]->text();
            $content = $this->downRemoutPic($document->find(".ed-dl")[0]->html());
            $navcontent = '';
            foreach ( $nav as  $item) {
              $navcontent .=$item->find("a")[0]->text().">";             
            }
            $match ="/<[\/]?[a|h1|h2|strong](.*)>/U";
            $content  = preg_replace($match, '', $content);
            $match ="/<p(.*)>/U";
            $content= preg_replace($match, '<p>', $content);           
              $createWebBaike =new WebBaike();
               $createWebBaike->bkid =$i;
               $createWebBaike->title =$title;
               $createWebBaike->url =$url;
               $createWebBaike->abstract =$abstract ;
               $createWebBaike->content =$content;
               $createWebBaike->nav =$navcontent;
               $createWebBaike->from =$from;
               $createWebBaike->save();
               break;
          }
 
      } 
    }
  

   private function downRemoutPic($content)
  {
    return $content;
  
    $path = public_path().'/case/';
    $localPath =public_path();
    preg_match_all('/src=\"(\S)+\"/', $content, $matchesArr);   
   
    foreach($matchesArr[0] as $url) {

      $url = substr($url, 5, -1);

      if(stripos($url, 'aliyuncs') === false) {
        if(strpos($url, 'http') === false) {
          //本地上传的
          // 上传到阿里云上, 删除本地文件
          $resPic = $this->toImageService($localPath . $url);
  
          @unlink($localPath . $url);
          //更新content里面的链接地址
          $content = str_replace($url, $resPic, $content); 
        } else {
          //远程上传的
    
          $tmpPic = file_get_contents($url);
       
          $tmpName = time() . rand(100,999) . rand(100,999);
          // 存入本地
          if(file_put_contents($path . $tmpName, $tmpPic)) {
            // 上传到阿里云上, 删除本地文件
            $resPic = $this->toImageService($path . $tmpName);
            @unlink($path . $tmpName);
            //更新content里面的链接地址
            $content = str_replace($url, $resPic, $content);
          } else {
            $content = str_replace($url, '', $content);
          }
        }
      }
    }  

    return $content;
  }  
  // 将图片通过curl传到图片服务器 $imgPath 带扩展名
  private function toImageService( $imgPath )
  {       
          //img_water_mark($imgPath,public_path().'/images/common/0.jpg');

          $mother = time();
          $fucker = md5( $mother . '394d722ab71dc3212180f4eb73b83919');
          $ch = curl_init('http://images.zhufaner.com/upload?mother='.$mother.'&fucker='.$fucker);
          $data['file'] = new \CURLFile( $imgPath );
          curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
          curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
          curl_setopt($ch, CURLOPT_POST,1);
          curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
          curl_setopt($ch, CURLOPT_TIMEOUT,5);
          $output = curl_exec($ch);
          if ( curl_errno($ch) == 28 || $output == 'error' ) {
                  $output = 0;
          }
          curl_close($ch);
          $url = 'https://zfnb-mis-imgs.oss-cn-beijing.aliyuncs.com/';
          $suffix = '.jpg';
          $resPic = $url . $output . $suffix;
          return $resPic;
  } 
   
}

